package org.market.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.market.domain.strategy.repository.IStrategyRepository;
import org.market.domain.strategy.service.armory.StrategyArmoryDispatch;
import org.market.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.market.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.market.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/23 下午11:26
 * @Version: 1.0
 */
@Slf4j
@Component("rule_blacklist")
public class BlacklistLogicChain extends AbstractLogicChain {
    /**
     * 查询ruleValue
     */
    @Resource
    private IStrategyRepository repository;

    @Resource
    private StrategyArmoryDispatch strategyArmoryDispatch;

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {

        log.info("责任链-黑名单过滤开始---userId:{},strategyId:{},ruleModel:{}", userId, strategyId,ruleModel());
        String ruleValue = repository.queryStrategyRuleValue(strategyId, ruleModel());

        String[] split = ruleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(split[0]);

        //规则过滤
        //100:blacklist_user_test01
        String[] blacklistUsers = split[1].split(Constants.SPLIT);

        for (String blacklistUserId : blacklistUsers) {
            if (blacklistUserId.equals(userId)){
                log.info("责任链-黑名单过滤检测到黑名单成员id---userId:{},strategyId:{}",userId,strategyId);
                return DefaultChainFactory.StrategyAwardVO.builder().awardId(awardId).build();
            }
        }
        log.info("责任链-黑名单过滤放行~~~");
        return next().logic(userId, strategyId);
    }

    @Override
    public String ruleModel() {
        return DefaultChainFactory.logicMode.RULE_BLACKLIST.getCode();
    }
}
